#!/usr/bin/env python
# encoding: utf-8
"""
@author: youfeng
@email: youfeng243@163.com
@license: Apache Licence 
@file: test_mongodb_read_data.py
@time: 2016/12/22 13:14
"""
import sys

from common import util
from common.config_parser import ConfigParser
from common.global_field import Model
from common.mongo import MongDb
from common.queue_mq_thread import MqQueueThread
from config.conf import mongo_db_target
from logger import Gsxtlogger

log = Gsxtlogger('send_data_to_mq.log', for_mat='').get_logger()

target_db = MongDb(mongo_db_target['host'], mongo_db_target['port'], mongo_db_target['db'],
                   mongo_db_target['username'], mongo_db_target['password'], log=log)

# 消息队列信息
beanstalk_server_conf = {
    # 测试IP
    'host': 'cs2.sz-internal.haizhi.com',
    'port': 11300,
    'tube': 'enterprise_download_rsp_test',
}


def send_detail(detail_data, company, queue_thread):
    count = 0
    if detail_data is not None:
        for detail_item in detail_data:
            url = detail_item.get('url', None)
            text = detail_item.get('text', None)

            # 不合规格的数据
            if not util.check_html(text):
                log.error('company = {company} url = {url}'.format(
                    url=url, company=company))
                continue

            if url is None or text is None:
                log.error('detail_item 读取字段详情error')
                continue

            queue_thread.push_msg(url, text, company=company)
            count += 1
    return count


def send_data(table, company_name=None):
    count = 0
    # 西藏益西农牧业科技发展有限公司
    # for item in target_db.traverse(table, {'datalist.change_info.list.2': {'$exists': 1}}):

    if company_name is None:
        select = {}
    else:
        select = {'_id': company_name}

    # 开启消息队列线程
    queue_thread = MqQueueThread(server_conf=beanstalk_server_conf, log=log)
    queue_thread.start()
    for item in target_db.traverse(table, select):

        company = item.get('_id', None)
        if company is None:
            continue

        data_list = item.get('datalist', None)
        if data_list is None:
            continue

        for key, value in data_list.iteritems():
            if key == Model.annual_info:
                continue
            if key == Model.contributive_info:
                continue
            if key == Model.liquidation_info:
                continue

            list_data = value.get(Model.type_list, None)
            count += send_detail(list_data, company, queue_thread)

    log.info('count = {cnt}'.format(cnt=count))
    queue_thread.close()
    queue_thread.join()


def send(province, company):
    conf_parse = ConfigParser('./config/cmb_gsxt_detail.conf')
    config_list = conf_parse.get_all_session()

    if province not in config_list:
        log.error('没有找到省份信息: province = {province}'.format(province=province))
        return

    target_table = config_list[province].get('target_table', None)
    if target_table is None:
        log.error('表信息错误..')
        return

    log.info('开始发送数据: province = {province} table = {table} company = {company}'.format(
        table=target_table, province=province, company=company))
    send_data(target_table, company)
    log.info('发送数据完成: province = {province} table = {table} company = {company}'.format(
        table=target_table, province=province, company=company))


def main():
    province = 'liaoning'
    company = None
    log.info('示例: python send_data_to_mq.py beijing 海致网络科技(北京)有限公司')
    if len(sys.argv) >= 2:
        province = sys.argv[1]
        if len(sys.argv) >= 3:
            company = sys.argv[2]

    send(province, company)


if __name__ == '__main__':
    main()
